<HTML><HEAD><TITLE>sort(+Key, +Order, +Random, -Sorted)</TITLE>
</HEAD><BODY>[ <A HREF="index.html">Comparing and Sorting</A> | <A HREF="../../index.html">Reference Manual</A> | <A HREF="../../fullindex.html">Alphabetic Index</A> ]
<H1>sort(+Key, +Order, +Random, -Sorted)</H1>
Succeeds if Sorted is the sorted list version of Random.  The sort is done
according to the Key and Order specifications.


<DL>
<DT><EM>Key</EM></DT>
<DD>A non-negative integer or a list of positive integers.
</DD>
<DT><EM>Order</EM></DT>
<DD>One of the atoms &lt;, =&lt;, &gt; or &gt;=.
</DD>
<DT><EM>Random</EM></DT>
<DD>List.
</DD>
<DT><EM>Sorted</EM></DT>
<DD>List or variable.
</DD>
</DL>
<H2>Description</H2>
   Sorts the list Random according to the Key and Order specifications, and
   unifies Sorted with the result.  The sort is stable, i.e. the order of
   elements with the same key is preserved.

<P>
   If Random is not a list of compound terms, use Key = 0.

<P>
   If Random is a list of compound terms, then the sort will be according
   to the Keyth argument of the list elements.  If Key = 0, then the sort
   is on the entire terms.

<P>
   In all cases, Order specifies whether the list is sorted into ascending
   (&lt;, =&lt;) or descending (&gt;, &gt;=) order and whether duplicates are to be
   retained (=&lt;, &gt;=) or eliminated (&lt;, &gt;).  The way to remember the Order
   argument is that it is the relation which holds between adjacent
   elements in the result.

<P>
   The sort is done according to the standard ordering of terms.
   See compare/3 for the definition of this standard ordering.
   Note in particular that numbers are first ordered by their type (integer,
   float, etc) and only then by their magnitude, i.e. sorting numbers of
   different types may not give the expected result.

<P>

<H3>Modes and Determinism</H3><UL>
<LI>sort(+, +, +, -) is det
</UL>
<H3>Exceptions</H3>
<DL>
<DT><EM>(5) type error </EM>
<DD>Key is greater than 0, and one of List1 and List2 does not    have all elements compound terms.
<DT><EM>(5) type error </EM>
<DD>Key is not an integer or a list of integers.
<DT><EM>(6) out of range </EM>
<DD>One of the compound terms in List1 or List2 has not got as    many as Key arguments.
</DL>
<H2>Examples</H2>
<PRE>
Success:
      sort(0,&lt;,[3,1,6,7,2],S).     (gives S=[1,2,3,6,7]).
      sort(0,&gt;,[q,1,3,a,e,N],S).
                           (gives N=_g78,S=[q,e,a,3,1,_g78]).
      sort(0,=&lt;,[1,3,2,3,4,1],S).  (gives S=[1,1,2,3,3,4]).
      sort(2,&lt;,[f(1,3),h(2,1)],S). (gives S=[h(2,1),f(1,3)]).
      sort(1,&lt;,[f(1,3),h(2,1)],S). (gives S=[f(1,3),h(2,1)]).
      sort([2,1],=&lt;,[f(3,a(2)),f(1,a(1)),f(0,a(3)),f(1,a(4))],S).
                           (gives S=[f(1,a(1)),f(3,a(2)),f(0,a(3)),f(1,a(4))]).

Fail:
      sort(0,&lt;,[2,1,3,4],[2,1,3,4]).

Error:
      sort(1,&lt;,[f(1),f(3),5],S).        (Error 5).
      sort(1.0,&lt;,[f(1),f(3),f(5)],S).   (Error 5).
      sort(2,&lt;,[f(1,2),g(3,a),f(5)],S). (Error 6).
</PRE>
<H2>See Also</H2>
<A HREF="../../kernel/termcomp/compare-3.html">compare / 3</A>, <A HREF="../../kernel/termcomp/msort-2.html">msort / 2</A>, <A HREF="../../kernel/termcomp/sort-2.html">sort / 2</A>, <A HREF="../../kernel/termcomp/number_sort-4.html">number_sort / 4</A>
</BODY></HTML>
